﻿using EntityPet;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PET.Tools;
using System.Security.Principal;

namespace PET.Controllers
{
    /// <summary>
    /// 登录界面
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class logonController : ControllerBase
    {
        private readonly PetDbContext db;
        public logonController(PetDbContext _context)
        {
            db = _context;

        }


        [HttpGet]
        public List<Pet_logon> GetBySelect([FromHeader] string Account, [FromHeader] string Email)
        {
            List<Pet_logon> logon = new List<Pet_logon>();

            if (string.IsNullOrEmpty(Account))
            {
                logon = db.Pet_logon.Where(s => s.Account == Account).OrderBy(s => s.Account).ToList();
            }
            return logon;
        }

        /// <summary>
        /// 查询用户信息
        /// </summary>
        /// <returns></returns>
        public Result Get()
        {
            Result result = new Result();

            var resulrData = db.Pet_logon.ToList<Pet_logon>();
            result.Data = resulrData;
            result.Code = 200;
            return result;
        }

        /// <summary>
        /// 添加用户信息
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>

        [HttpPost]
        public Result AddUser([FromBody] Pet_logon logon)
        {
            Result result = new Result();
            db.Pet_logon.Add(logon);
            result.Data = db.SaveChanges();
            return result;
        }


        /// <summary>
        /// 删除用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public Result DelUser(Guid id)
        {
            Result result = new Result();
            Pet_logon? user = db.Pet_logon.Where(w => w.ID == id).SingleOrDefault();
            if (user != null)
                db.Pet_logon.Remove(user);
            result.Data = db.SaveChanges();
            return result;
        }

        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPut]
        public Result UpUser([FromBody]Pet_logon login)
        {
            Result result = new Result();
            db.Pet_logon.Update(login);
            result.Data = db.SaveChanges();
            return result;
        }
    }
}
